{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "'周瑜'的词向量为: \n",
      " [-0.01039489 -0.03386581  0.74343777  0.31893775  0.05329462 -0.7149288\n",
      "  0.11086965  0.7218369  -1.0500177  -0.01257292 -0.2502782  -0.48040336\n",
      "  0.3508377   0.19167927  1.1770914   0.0581043   1.084993   -0.2771976\n",
      " -0.24237387 -0.45486498]\n",
      "与'周瑜'相似度最高的10个歌词\n",
      "[('孙策', 0.9514368176460266), ('袁术', 0.9355677366256714), ('邓艾', 0.9325023889541626), ('孟获', 0.9281263947486877), ('魏主', 0.9277908802032471), ('钟会', 0.9275678396224976), ('吴侯', 0.920265793800354), ('陆逊', 0.9183912873268127), ('孙权', 0.917974591255188), ('吕布', 0.9146259427070618)]\n",
      "'刘备'和'曹操'的相似度:0.7698577642440796\n",
      "在词'孙权/曹操/刘备/孙夫人中','刘备'与其他词不属于同一类\n"
     ]
    }
   ],
   "source": [
    "import jieba\n",
    "import re\n",
    "from gensim.models import Word2Vec\n",
    "with open(r'sanguo.txt',encoding='utf-8') as f:\n",
    "    lines=[]\n",
    "    for line in f:\n",
    "        temp=jieba.lcut(line)\n",
    "        words=[]\n",
    "        for i in temp:\n",
    "            i=re.sub('i = re.sub(\"[\\s+\\.\\!\\/_,$%^*(+\\\"\\'””《》]+|[+——！，。？、~@#￥%……&*（）：；‘]+\", \"\", i)','',i)\n",
    "            if len(i)>0:\n",
    "                words.append(i)\n",
    "        if len(words)>0:\n",
    "            lines.append(words)\n",
    "model=Word2Vec(lines,vector_size=20,window=2,min_count=3,epochs=7,negative=10,sg=1)\n",
    "print(\"'周瑜'的词向量为: \\n\",model.wv.get_vector('周瑜'))\n",
    "print(\"与'周瑜'相似度最高的10个歌词\")\n",
    "print(model.wv.most_similar('周瑜',topn=10))\n",
    "print(\"'刘备'和'曹操'的相似度:{}\".format(model.wv.similarity('刘备','曹操')))\n",
    "words='孙权 曹操 刘备 孙夫人'\n",
    "print(\"在词'孙权/曹操/刘备/孙夫人中','{}'与其他词不属于同一类\".format(model.wv.doesnt_match(words.split())))\n",
    "            \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import gensim\n",
    "from gensim.models.doc2vec import Doc2Vec\n",
    "TaggededDocument = gensim.models.doc2vec.TaggedDocument\n",
    "def get_data():\n",
    "    with open(\"train.txt\",\"r\",encoding = \"utf-8\") as f:\n",
    "        docs = f.readlines()\n",
    "        train_data = []\n",
    "    for i,text in enumerate(docs):\n",
    "        word_list = text.split(\" \")\n",
    "        word_list[len(word_list)-1] = word_list[len(word_list)-1].strip()\n",
    "        document = TaggededDocument(word_list,tags = [i])\n",
    "        train_data.append(document)\n",
    "    return train_data\n",
    "def train_model(x_train):\n",
    "    model_dm=Doc2Vec(x_train,min_count = 1,window = 3,vector_size = 20,negative = 5,workers = 4,dm = 1 )\n",
    "    model_dm.train(x_train, total_exampleds = model_dm.corpus_count,epochs = 70)\n",
    "    model_dm.save('model_doc2vec')\n",
    "    return model_dm\n",
    "def test():\n",
    "    model_dm = Doc2Vec.load('model_doc2vec')\n",
    "    test_text = ['科学','教育','是','难搞','的']\n",
    "    inferred_vertor_dm = model_dm.infer_vertor(test_text)\n",
    "    sims = model_dm.docvecs.most_similar([inferred_vertor_dm],topn = 10)\n",
    "    return sims\n",
    "if __name__ =='__main__':\n",
    "    train_data = get_data()\n",
    "    model_dm = train_model(train_data)\n",
    "    sims = test()\n",
    "    print('相似文本，相似度和文本中词的数量:')\n",
    "    for count,sim in sims:\n",
    "        sentence = train_data[count]\n",
    "        words = ''\n",
    "        for word in sentence[0]:\n",
    "            words = words + word + ''\n",
    "        print(words,sim,len(sentence))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
